package com.tusuo.stds.ui.integrity;

import android.content.Context;
import android.opengl.Visibility;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.Complete;

import java.util.ArrayList;

/**
 * Created by developer on 17-8-30.
 */

public class IntegrityAdapter extends BaseAdapter {
    private Context mContext;
    private Complete mComplete;
    private ArrayList<Complete.GroupItem> mData;
    private ArrayList<Integer> mBackgroudResources;
    private ArrayList<String> mTitles;


    public IntegrityAdapter(Context context, Complete complete) {
        mContext = context;
        mComplete = complete;
        mData = new ArrayList<>();

        if (checkData()) {
            mData.addAll(mComplete.ProblemList);
            mData.addAll(mComplete.MissList);
            mData.addAll(mComplete.PassList);
        }
        initData();
    }

    private void initData() {
        mBackgroudResources = new ArrayList<>();
        mTitles = new ArrayList<>();
        for (int i = 0; i < mData.size(); i ++) {
            initBackground(i);
            initTitle(i);
        }
    }

    private void initBackground(int index) {

        if (index < mComplete.ProblemList.size()) {
            // 问题
            mBackgroudResources.add(R.drawable.shape_integrity_question);
        }else if (index >= mComplete.ProblemList.size() && index < mComplete.ProblemList.size() + mComplete.MissList.size()) {
            // 模糊
            mBackgroudResources.add(R.drawable.shape_integrity_blurry);
        }else {
            // 合格
            mBackgroudResources.add(R.drawable.shape_integrity_qualified);
        }
    }

    private void initTitle(int index) {

        if (index == 0 && mComplete.ProblemList.size() > 0) {
            // 显示第一个集合的标题
            mTitles.add("需要加强训练的");
        }else if (index == mComplete.ProblemList.size() && mComplete.MissList.size() > 0) {
            // 显示第二个集合的标题
            mTitles.add("需要多练几次以协助评估的");
        }else if (index == mComplete.ProblemList.size() + mComplete.MissList.size() && mComplete.PassList.size() > 0) {
            // 显示第三个集合的标题
            mTitles.add("已经掌握的");
        }else {
            mTitles.add("");
        }

    }

    private boolean checkData() {
        return mComplete != null && mComplete.MissList != null && mComplete.PassList != null && mComplete.ProblemList != null;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return mData.get(position).Id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;

        if (convertView == null) {
            view = View.inflate(mContext,R.layout.item_integrity_list,null);
            holder = new ViewHolder();
            holder.tvType = (TextView) view.findViewById(R.id.item_integrity_tv_type);
            holder.tvDescriber = (TextView) view.findViewById(R.id.item_integrity_tv_describer);
            holder.layoutBackground = (LinearLayout) view.findViewById(R.id.item_integrity_layout_background);
            holder.viewDivider = view.findViewById(R.id.item_integrity_view_divider);
            view.setTag(holder);
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }


        holder.tvDescriber.setText(mData.get(position).Name);

        holder.layoutBackground.setBackgroundResource(mBackgroudResources.get(position));

        if (position == 0 && mComplete.ProblemList.size() > 0) {
            // 显示第一个集合的标题
            holder.tvType.setVisibility(View.VISIBLE);
        }else if (position == mComplete.ProblemList.size() && mComplete.MissList.size() > 0) {
            // 显示第二个集合的标题
            holder.tvType.setVisibility(View.VISIBLE);
        }else if (position == mComplete.ProblemList.size() + mComplete.MissList.size() && mComplete.PassList.size() > 0) {
            // 显示第三个集合的标题
            holder.tvType.setVisibility(View.VISIBLE);
        }else {
            holder.tvType.setVisibility(View.GONE);
        }

        holder.tvType.setText(mTitles.get(position));


        if (position == mComplete.ProblemList.size() - 1 && mComplete.ProblemList.size() > 0) {
            // 第一个集合的最后一个元素
            holder.viewDivider.setVisibility(View.VISIBLE);
        }else if (position == mComplete.ProblemList.size() + mComplete.MissList.size() - 1 && mComplete.MissList.size() > 0) {
            // 第二个集合的最后一个元素
            holder.viewDivider.setVisibility(View.VISIBLE);
        }else {
            holder.viewDivider.setVisibility(View.GONE);
        }


        return view;
    }

    class ViewHolder{
        TextView tvType;
        TextView tvDescriber;
        LinearLayout layoutBackground;
        View viewDivider;
    }

}
